package com.bussiness.xutai.controller;

import com.bussiness.xutai.bean.Product;
import com.bussiness.xutai.bean.RespBean;
import com.bussiness.xutai.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping("select_product")
    @ResponseBody
    public List<Product> select_product() {
        return productService.getAll();
    }

    @RequestMapping("queryById_product")
    @ResponseBody
    public Product queryById_product(Product product) {
        return productService.getById(product);
    }

    @RequestMapping("update_product")
    @ResponseBody
    public RespBean update_product(Product product) {
        boolean updateFlag = productService.updateProduct(product);
        if (updateFlag) {
            return RespBean.ok("修改成功");
        } else {
            return RespBean.error("修改失败");
        }
    }

    @RequestMapping("save_product")
    @ResponseBody
    public RespBean save_product(Product product) {
        boolean saveFlag = productService.saveProduct(product);
        if (saveFlag) {
            return RespBean.ok("添加成功");
        } else {
            return RespBean.error("添加失败");
        }
    }

    @RequestMapping("delete_product")
    @ResponseBody
    public RespBean delete_product(Product product) {
        boolean deleteFlag = productService.removeProduct(product);
        if (deleteFlag) {
            return RespBean.ok("删除成功");
        } else {
            return RespBean.error("删除失败");
        }
    }

    @RequestMapping("myPage_product")
    @ResponseBody
    public Map<String, Object> myPage_product(int page, int rows) {
        List<Product> list = productService.getSize(page, rows);
        Map<String, Object> map = new HashMap<>();
        map.put("total", productService.getCount());
        map.put("rows", list);
        return map;
    }
}